﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNet.Highcharts.Options;
using DotNet.Highcharts.Helpers;
using BootstrapExemp.model;

namespace BootstrapExemp
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Grafico(0,0);
             }
        }

        private void Grafico(int qtdHomem, int qtdMulher)
        {
            Series s1 = new Series() { Data = new Data(new object[] { 29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4 + qtdHomem }), Name = "Homens" };
            Series s2 = new Series() { Data = new Data(new object[] { 40.9, 67, 54.6, 85.9, 72.5, 123.9, 112.5, 99.6, 85.7, 201.9, 56.9, 124.6 + qtdMulher}), Name = "Mulheres" };
            List<Series> listSeries = new List<Series>();
            listSeries.Add(s1);
            listSeries.Add(s2);

            DotNet.Highcharts.Highcharts grafico = new DotNet.Highcharts.Highcharts("chart")
                .SetXAxis(new XAxis { Categories = new[] { "Jan", "Fev", "Mar", "Abr", "Mai", "Jun", "Jul", "Ago", "Set", "Out", "Nov", "Dez" } })
                .SetSeries(listSeries.ToArray());

            grafico.SetTitle(new DotNet.Highcharts.Options.Title() { Text = "Clientes" });
            grafico.SetSubtitle(new Subtitle() { Text = "Cadastro realizado no site" });
            grafico.SetYAxis(new YAxis() { Title = new YAxisTitle() { Text = "Quantidades" } });

            ltrChart.Text = grafico.ToHtmlString();
        }

        protected void Enviar_Click(object sender, EventArgs e)
        {
            Usuario usu = new Usuario();
            bool validouForm = true;

            #region Validações
            //Nome
            if (this.txtInpNome.Text.Equals(""))
            {
                validouForm = false;
                this.iconErroNome.Visible = true;
            }
            else
            {
                usu.Nome = this.txtInpNome.Text;
                this.iconErroNome.Visible = false;
            }

            //Sobrenome
            if (this.txtInpSobrenome.Value.Equals(""))
            {
                validouForm = false;
                this.iconErroSobrenome.Visible = true;
            }
            else
            {
                usu.Sobrenome = this.txtInpSobrenome.Value;
                this.iconErroSobrenome.Visible = false;
            }

            //Senha
            if (this.passwordInputSenha.Value.Equals(""))
            {
                validouForm = false;
                this.iconErroSenha.Visible = true;
            }
            else
            {
                usu.Senha = this.passwordInputSenha.Value;
                this.iconErroSenha.Visible = false;
            }

            //ConfirmarSenha
            if (this.passwordInputConfirmarSenha.Value.Equals(""))
            {
                validouForm = false;
                this.iconErroConfirmarSenha.Visible = true;
            }
            else
            {
                usu.ConfirmarSenha = this.passwordInputConfirmarSenha.Value;
                this.iconErroConfirmarSenha.Visible = false;
            }

            //Sexo
            usu.Sexo = this.radioSexo.SelectedValue;

            //Conechimento
            usu.Conhecimento = this.selectbasicConhecimento.Value;

            //Observacoes
            usu.Observacoes = this.textareaObservacao.Value;

            //Informacoes
            usu.ListInteresses = new List<string>();
            List<string> listInteresse = new List<string>();
            foreach (Control x in this.Controls)
            {
                if (x is CheckBox)
                {
                    if (((CheckBox)x).Checked == true)
                        usu.ListInteresses.Add(((CheckBox)x).Text);
                }
            }
            #endregion

            if (validouForm)
            {
                Session["usuario"] = usu;
                this.divPanel.Visible = false;
                this.divFormularioCompleto.Visible = true;

                if (usu.Sexo.ToString().Equals("Masculino"))
                    Grafico(1, 0);
                else
                    Grafico(0, 1);

                

            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertaFormulario",
                                                        "alert('Alguns campos não foram preenchidos corretamente. Favor verificar o formulário.')", true);
            }
            //this.divFormulario.Visible = false;

            this.updatePanelForm.Update();
        }

        protected void Limpar_Click(object sender, EventArgs e)
        {
            ClearFields();
            this.iconErroNome.Visible = false;
            this.iconErroSobrenome.Visible = false;
            this.iconErroSenha.Visible = false;
            this.iconErroConfirmarSenha.Visible = false;
            this.updatePanelForm.Update();
        }

        protected void NovoUsuario_Click(object sender, EventArgs e)
        {
            this.divPanel.Visible = true;
            this.divFormularioCompleto.Visible = false;
            
            ClearFields();
            this.updatePanelForm.Update();
        }

        private void ClearFields()
        {
            this.txtInpNome.Text = string.Empty;
            this.txtInpSobrenome.Value = string.Empty;
            this.passwordInputSenha.Value = string.Empty;
            this.passwordInputConfirmarSenha.Value = string.Empty;
            this.radioSexo.SelectedIndex = 0;
            this.selectbasicConhecimento.SelectedIndex = 0;
            this.textareaObservacao.Value = string.Empty;

            foreach (Control x in this.Controls)
            {
                if (x is CheckBox)
                {
                    if (((CheckBox)x).Checked == true)
                        ((CheckBox)x).Checked = false;
                }
            }
        }

        protected void teste_TextChanged(object sender, EventArgs e)
        {
            if (!this.txtInpNome.Text.Equals(""))
            {
                
            }
        }
    }
}